package com.example.lin.ximalaya.adapter.viewholder;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.example.lin.ximalaya.R;
import com.example.lin.ximalaya.pojo.HotData;
import com.example.lin.ximalaya.pojo.Type24listItem;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class Type24ViewHolder extends RecyclerView.ViewHolder {

    private Context context;

    @BindView(R.id.tv_ting1)
    TextView tvTing1;

    @BindView(R.id.tv_ting2)
    TextView tvTing2;

    @BindView(R.id.tv_more)
    TextView tvMore;

    public Type24ViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        ButterKnife.bind(this, itemView);
        initView();
    }

    private void initView() {

    }

    ArrayList<Type24listItem> listItems;

    /**
     * 为 ViewHolder 绑定数据
     *
     * @param data 数据对象
     */
    public void setData(HotData.ListBeanX data) {
        List<JsonObject> list = data.getList();
        listItems = new ArrayList<>();
        for (JsonObject jsonObject : list) {
            Type24listItem type24listItem = new Gson().fromJson(jsonObject, Type24listItem.class);
            listItems.add(type24listItem);
        }
        startLoop();
    }

    private TextView currentTv;
    private TextView nextTv;

    private int nextIndex = 0;

    private void startLoop() {
        h.removeMessages(0);
        currentTv = tvTing1;
        nextTv = tvTing2;
        currentTv.setText(listItems.get(nextIndex).getTitle());
        h.sendEmptyMessageDelayed(0, 3000);
    }


    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    nextIndex = (nextIndex + 1) % listItems.size();
                    nextTv.setText(listItems.get(nextIndex).getTitle());
                    nextTv.setVisibility(View.VISIBLE);
                    //TODO 开启动画,让 currentTv 从上面移出去,而 nextTv从下面移进来
                    Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_top_out);
                    animation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            currentTv.setVisibility(View.GONE);
                            final TextView tag = currentTv;
                            currentTv = nextTv;
                            nextTv = tag;

                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    currentTv.startAnimation(animation);
                    nextTv.startAnimation(AnimationUtils.loadAnimation(context, R.anim.anim_bottom_in));
                    h.sendEmptyMessageDelayed(0, 3000);
                    break;
            }
            return false;
        }
    });

}
